using UnityEngine;
using System.Collections;

public class PlayerRanged : MonoBehaviour {
	
	public int lifes = 3;
	public static float HP = 10;
	private string strHP;
	public Connection conn;
	public GameObject sun;
	public Transform flashlight;
	
	// Use this for initialization
	void Start () 
	{
		foreach(Transform child in transform)
		{
			if(child.name == "Spotlight")
			{
				flashlight = child;
			}
		}
		
		strHP = "HP:IIIIIIIIII";
		conn = GameObject.FindWithTag("MainCamera").GetComponent<Connection>();
		sun = GameObject.FindWithTag("Sun");
		
		CheckDifficulty();
	}
	
	// Update is called once per frame
	void Update () 
	{
		animation.Play("Andar");
		
		if(networkView.isMine)
		{
			if(HP <= 0)
			{
				lifes--;
				
				if(lifes > 0)
				{
					transform.position = new Vector3(Random.Range(-13.5f,25), 1, Random.Range(-20,19));
					HP = 10;
					strHP = "HP:IIIIIIIIII";
				}
				
				if(lifes < 0)
				{
					Application.LoadLevel("Lost");
				}
			}
			
			if(rigidbody.velocity.magnitude < 6)
			{
				if(Input.GetKey("w"))
				{
					animation.Play("Andar");
					rigidbody.AddForce(Vector3.forward * 10);
				}
			
				if(Input.GetKey("a"))
				{
					animation.Play("Andar");
					rigidbody.AddForce(-Vector3.right * 10);
				}
			
				if(Input.GetKey("s"))
				{
					animation.Play("Andar");
					rigidbody.AddForce(-Vector3.forward * 10);
				}
			
				if(Input.GetKey("d"))
				{
					animation.Play("Andar");
					rigidbody.AddForce(Vector3.right * 10);
				}
				
				if(Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") || Input.GetKeyUp("d"))
				{
					animation.Stop();
				}
			}
		}
	}
	
	public void LostHP()
	{
		if(networkView.isMine)
		{
			HP -= 1;
			string newHP = strHP.Remove(strHP.Length - 1);
			strHP = newHP;
		}
	}
	
	public void CheckDifficulty()
	{
		if(conn.easyMode)
		{
			flashlight.gameObject.active = false;
			sun.active = true;
		}
		
		if(conn.hardMode)
		{
			flashlight.gameObject.active = true;
			sun.active = false;
		}
	}
	
	public void OnGUI()
	{
		if(networkView.isMine)
		{
			GUI.Label(new Rect(10, Screen.height-100, 100, 150), "Lifes:" + lifes);
			GUI.Label(new Rect(10, Screen.height-50, 100, 150), "Ammo:" + RangedGun.ammo);
			GUI.Label(new Rect(Screen.width/2, Screen.height/2, 150, 150), strHP);
		}
	}
	
	void OnDisconnectedFromServer(NetworkDisconnection info) 
	{
        Debug.Log("Disconnected from server: " + info);
    }
}